package com.itheima.mall.web.pms;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.pms.PmsProductCategoryService;
import com.itheima.mall.entity.domain.pms.ProductCategory;
import com.itheima.mall.entity.dto.ProductCategoryDto;
import com.itheima.mall.entity.page.MyPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {
    @DubboReference
    private PmsProductCategoryService pmsProductCategoryService;
    //产品分类查询
    @GetMapping("/list/{parentId}")
    public Result list(@PathVariable Integer parentId,Integer pageNum,Integer pageSize){
      MyPage<ProductCategory> categoryPage = pmsProductCategoryService.getPage(parentId, pageNum,pageSize);
       return Result.success(categoryPage);
    }
    //修改导航栏显示状态
    @PostMapping("update/navStatus")
    public Result navStatus(@RequestParam List<Long> ids,@RequestParam Integer navStatus){
        List<ProductCategory> categoryList = pmsProductCategoryService.listByIds(ids);
        for (ProductCategory productCategory : categoryList) {
            productCategory.setNavStatus(navStatus);
            pmsProductCategoryService.updateById(productCategory);
        }
        return Result.success("修改成功");
    }
    //修改显示状态
    @PostMapping("update/showStatus")
    public Result showStatus(@RequestParam List<Long> ids,@RequestParam Integer showStatus){
        List<ProductCategory> categoryList = pmsProductCategoryService.listByIds(ids);
        for (ProductCategory productCategory : categoryList) {
            productCategory.setShowStatus(showStatus);
            pmsProductCategoryService.updateById(productCategory);
        }
        return Result.success("修改成功");
    }
    //删除商品分类
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id){
        pmsProductCategoryService.removeById(id);
        return Result.success("删除成功");
    }
    //查询所有一级子类及分类
    @GetMapping("list/withChildren")
    public Result listTow(){
        List<ProductCategory> categoryList =  pmsProductCategoryService.getByOne();
        List<ProductCategoryDto> productCategoryDtos = new ArrayList<>();
        for (ProductCategory productCategory : categoryList) {
            Long id = productCategory.getId();
            List<ProductCategory> categoryList2 =  pmsProductCategoryService.getByTow(id);
            ProductCategoryDto category = new ProductCategoryDto();
            BeanUtils.copyProperties(productCategory,category);
            category.setChildren(categoryList2);
            productCategoryDtos.add(category);
        }
        return Result.success(productCategoryDtos);
    }
    //根据ID获取商品分类
    @GetMapping("/{id}")
    public Result getById( Integer id){
        ProductCategory productCategory = pmsProductCategoryService.getById(id);
        return Result.success(productCategory);
    }
    //修改商品分类
    @PostMapping("/update/{id}")
    public Result update(@RequestParam Integer id,@RequestBody ProductCategory productCategory){
        pmsProductCategoryService.updateById(productCategory);
        return Result.success("修改成功");
    }
    //添加商品分类
    @PostMapping("/create")
    public Result create(@RequestBody ProductCategory productCategory){
        pmsProductCategoryService.save(productCategory);
        return Result.success("添加成功");
    }
}
